package common.serialization;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;


import util.StringUtil;

public class XmlDataBean implements Serializable, IWebText {
	private static final long serialVersionUID = 1L;
	public static final String NODE = "N";// 节点名称
	public static final String OPERATOR = "O";// 操作类型;
	public static final String OPERATOR_INSERT_BROTHER = "ib";// 插入兄弟记录
	public static final String OPERATOR_INSERT_CHILD = "ic";// 插入子结点
	public static final String OPERATOR_UPDATE = "u";// 修改
	public static final String OPERATOR_DELETE = "d";// 删除
	public static final String OPERATOR_FIND = "f";// 查找
	public static final String OPERATOR_MODULE = "M";// 模块名
	public static final String OPERATOR_INIT = "init";// 初始
	public static final String OPERATOR_EMPTY = "e";// 空白
	public static final String OPERATOR_ERROR = "error"; // /O="error" 错误 信息标志
	public static final String OPERATOR_MESSAGE = "message"; // /错误 信息提示
	public static final String OPERATOR_SUCCESS = "true"; // /成功 信息提示

	// public final static String ERROR="error"; ///O="error" 错误 信息标志
	// public final static String MESSAGE="message"; ///错误 信息提示
	public static final String OPERATOR_JavaToFlexComponent = "JavaToFlexComponent";// JAVA发送到FLEX的组件标识
	public static final String OPERATOR_JavaToFlexComponen_InitValue = "initValue";// 初始化值

	// ----------------------------------------------
	// public static final String OPERATOR_FLEX_ALERT="alert";
	// public static final String OPERATOR_FLEX_CONFIRM="confirm";
	// public static final String OPERATOR_FLEX_PROMPT="prompt";
	// --------------------------------------------------------
	// --------------------------------XML=记录结点标识----------------
	public final static String XML_CHILD = "child";
	public final static String XML_CHILD_MAYBE = "maybe";
	public final static String XML_CHILD_OPEN = "open";
	public final static String XML_CHILD_NO = "no";

	protected String id;// 关键字(唯一)
	protected String nameSpace = NODE;// 节点名称<N></N>
	protected Map<String, String> attribMap = new HashMap<String, String>();// 扩展属性
																			// <node
																			// a1="bb"
																			// b1="cc"/>
	protected XmlDataBean parentDataBean;
	protected List<XmlDataBean> childXmlDataBeanList = new ArrayList<XmlDataBean>();
	protected String text = null;// 文本内容

	public Map<String, String> getAttribMap() {
		return attribMap;
	}

	public void setAttribMap(Map<String, String> attribMap) {
		this.attribMap = attribMap;
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

	public void setAttribute(String key, String value) {
		attribMap.put(key, value);
	}

	public String getAttribute(String key) {
		return (String) attribMap.get(key);
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public XmlDataBean getParentDataBean() {
		return parentDataBean;
	}

	public void setParentDataBean(XmlDataBean parentDataBean) {
		this.parentDataBean = parentDataBean;
	}

	public List<XmlDataBean> getChildXmlDataBeanList() {
		return childXmlDataBeanList;
	}

	public void setChildXmlDataBeanList(List<XmlDataBean> childXmlDataBeanList) {
		this.childXmlDataBeanList = childXmlDataBeanList;
	}

	// 追加插入
	public void appendChildXmlDataBean(XmlDataBean xmlDataBea) {

		xmlDataBea.setParentDataBean(this);
		childXmlDataBeanList.add(xmlDataBea);
	}

	// 插入结点之后
	public void insertChildAfterXmlDataBean(String findid,
			XmlDataBean xmlDataBea) {
		int len = childXmlDataBeanList.size();
		for (int i = 0; i < len; i++) {
			XmlDataBean xmlDataBean = (XmlDataBean) childXmlDataBeanList.get(i);
			if (findid.equals(xmlDataBean.getId())) {
				xmlDataBea.setParentDataBean(this);
				childXmlDataBeanList.add(i, xmlDataBea);
				return;
			}
		}
		appendChildXmlDataBean(xmlDataBea);
	}

	// 查换结点
	public XmlDataBean findXmlChildDataBean(String findid) {
		if (this.id.equals(findid)) {
			return this;
		}
		int len = childXmlDataBeanList.size();
		for (int i = 0; i < len; i++) {
			XmlDataBean xmlDataBean = (XmlDataBean) childXmlDataBeanList.get(i);
			if (findid.equals(xmlDataBean.getId())) {
				return xmlDataBean;
			}

			xmlDataBean = xmlDataBean.findXmlChildDataBean(findid);

			if (xmlDataBean != null) {
				return xmlDataBean;
			}

		}
		return null;
	}

	// 查找结点[使用此方法时，最根结点的id值一定要存在，不然报NULL错]
	public XmlDataBean findXmlDataBean(String findid) {

		XmlDataBean p = this;
		while (p.getParentDataBean() != null) {
			p = p.getParentDataBean();
		}
		return p.findXmlChildDataBean(findid);
	}

	public String getNameSpace() {
		return nameSpace;
	}

	public void setNameSpace(String nameSpace) {
		this.nameSpace = nameSpace;
	}

	/*
	 * 
	 * <node id="2" name="Adobe" type="COMPANY" desc="公司"
	 * statusIcon="statusIcon" ratio="30.7"> <node id="5"
	 * name="Adobe Consulting" type="COMPANY" desc="Adobe (formerly macromedia)"
	 * statusIcon="statusIcon" ratio="20.6"/> <node id="6" name="EDBU"
	 * type="COMPANY" desc="Database company" statusIcon="statusIcon"
	 * ratio="10.9"> <node name="FLEX1" type="SOLUTION"
	 * desc="Plus Business Solution" statusIcon="" ratio="10.9" /> <node
	 * name="FLEX2" type="SOLUTION" desc="Plus Business Solution" statusIcon=""
	 * ratio="10.9"/> <node name="FLEX3" type="SOLUTION"
	 * desc="Plus Business Solution" statusIcon="" ratio="10.9"/> <node
	 * name="FLEX4" type="SOLUTION" desc="Plus Business Solution" statusIcon=""
	 * ratio="10.9"/> <node name="FLEX5" type="SOLUTION"
	 * desc="Plus Business Solution" statusIcon="" ratio="10.9"/> </node>
	 * </node>
	 */
	public String toXMLStrig() throws Exception {
		StringBuffer sb = new StringBuffer(500);
		sb.append("<").append(nameSpace).append(" id=\"")
				.append(StringUtil.string2Html(id)).append("\"");
		if (attribMap != null) {
			Iterator it = attribMap.keySet().iterator();
			while (it.hasNext()) {
				String key = (String) it.next();
				sb.append(" ")
						.append(key)
						.append("=\"")
						.append(StringUtil.string2Html((String) attribMap
								.get(key))).append("\"");
				;
			}
		}
		if (childXmlDataBeanList == null || childXmlDataBeanList.size() == 0) {
			if (text != null) {
				sb.append(">");
				sb.append("<![CDATA[");
				sb.append(text);
				sb.append("]]>");
				sb.append("</").append(nameSpace).append(">");
			} else {
				sb.append("/>");
			}
			return sb.toString();
		}
		sb.append(">");
		int len = childXmlDataBeanList.size();
		for (int i = 0; i < len; i++) {
			XmlDataBean xmlDataBean = (XmlDataBean) childXmlDataBeanList.get(i);
			sb.append(xmlDataBean.toXMLStrig());
		}
		sb.append("</").append(nameSpace).append(">");

		return sb.toString();
	}

	public String outWebText() throws Exception {
		return toXMLStrig();
	}

	public static void main(String[] arg) throws Exception {
		XmlDataBean root = new XmlDataBean();
		root.setNameSpace("xs:element");
		root.setAttribute("name", "ConstructProject");
		root.setText("333333");
		/*
		 * XmlDataBean xdb1 = new XmlDataBean(); xdb1.setId("2");
		 * xdb1.setText("#####&&&&&&123213"); root.appendChildXmlDataBean(xdb1);
		 */
		System.out.println(root.outWebText());

	}

}
